package com.fivefu.core.camera.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.camera.entity.DbMonitorRegion;
import com.fivefu.core.camera.services.CameraTreeService;
import com.fivefu.core.camera.services.DbMonitorRegionService;
import com.fivefu.core.camera.vo.response.ProPageList;
import com.fivefu.core.data.vo.TreeVo;
import com.fivefu.core.module.auth.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fivefu.base.web.controller.BaseController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 自定义探头组织 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-27 15:14:41
 */
@Api(tags = "探头结构管理")
@RestController
@RequestMapping("/monitorRegion")
public class DbMonitorRegionController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(DbMonitorRegionController.class);

    @Autowired
    private DbMonitorRegionService dbMonitorRegionService;
    @Autowired
    private CameraTreeService cameraTreeService;

    @ApiOperation(value = "编辑文件夹",httpMethod = "POST",response = ResultInfo.class,notes = "编辑文件夹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "regionName",value = "文件分类名",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "pid",value = "父级文件组织编号",paramType = "query",required = false, dataType = "Long"),
            @ApiImplicitParam(name = "id",value = "文件组织id",paramType = "query",required = false, dataType = "Long"),
    })
    @RequestMapping(path = "/editorMonitorRegion",method = RequestMethod.POST)
    public ResultInfo editorMonitorRegion(String regionName,Long pid,Long id, @RequestAttribute String userid) {
        try {
            logger.info("编辑文件夹接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (StrUtils.isNull(regionName)) {
                return ResultInfo.renderError("分类名称不能为空");
            }
            if (null == pid) {
                pid = 0L;
            }
            DbMonitorRegion monitorRegion = dbMonitorRegionService.queryMonitorRegionByRegionName(regionName,pid,null);
            if (null != monitorRegion ) {
                if (id != null && monitorRegion.getId().equals(id)){
                    return ResultInfo.renderError("文件夹名称未修改");
                }
                return ResultInfo.renderError("编辑文件夹失败");
            }

            dbMonitorRegionService.editMonitorRegion(regionName,pid,id,userid);
            return ResultInfo.renderSuccess("编辑文件夹成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("编辑文件夹失败");
    }

    @ApiOperation(value = "获取所有探头管理树结构",httpMethod = "POST",response = ResultInfo.class,notes = "获取所有探头管理树结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "查询类型(list,tree)",paramType = "query",required = true, dataType = "String")
    })
    @RequestMapping(path = "/getMonitorRegionTreeStructure",method = RequestMethod.POST)
    public ResultInfo getMonitorRegionTreeStructure(String type) {
        try {
            logger.info("获取所有探头管理树结构接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (StrUtils.isNull(type)){
                return ResultInfo.renderError("查询类型不能为空");
            }
            List<TreeVo> treeList = cameraTreeService.getMonitorRegionTree(type);
            return ResultInfo.renderSuccess(treeList);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("获取所有探头管理树结构失败");
    }


    @ApiOperation(value = "文件夹下添加相关探头信息",httpMethod = "POST",response = ResultInfo.class,notes = "文件夹下添加相关探头信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文件id",paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "cameraCodes",value = "探头id（多个探头id用逗号隔开）",paramType = "query",required = true, dataType = "String"),
    })
    @RequestMapping(path = "/addMonitorRegionCameras",method = RequestMethod.POST)
    public ResultInfo addMonitorRegionCameras(Long id,String cameraCodes, @RequestAttribute String userid) {
        try {
            logger.info("文件夹下添加相关探头信息接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("文件id不能为空");
            }
            if (StrUtils.isNull(cameraCodes)) {
                return ResultInfo.renderError("探头id不能为空");
            }
            dbMonitorRegionService.addMonitorRegionCameras(id,cameraCodes,userid);
            return ResultInfo.renderSuccess("添加相关探头成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("添加相关探头失败");
    }

    @ApiOperation(value = "移除文件夹下探头",httpMethod = "POST",response = ResultInfo.class,notes = "移除文件夹下探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文件id",paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "cameraCode",value = "探头id",paramType = "query",required = true, dataType = "String"),
    })
    @RequestMapping(path = "/removeMonitorRegionCamera",method = RequestMethod.POST)
    public ResultInfo removeMonitorRegionCamera(Long id,String cameraCode, @RequestAttribute String userid) {
        try {
            logger.info("移除文件夹下探头接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("文件id不能为空");
            }
            if (StrUtils.isNull(cameraCode)) {
                return ResultInfo.renderError("探头id不能为空");
            }
            dbMonitorRegionService.removeMonitorRegionCamera(id,cameraCode,userid);
            return ResultInfo.renderSuccess("移除文件夹下探头成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("移除文件夹下探头失败");
    }

    @ApiOperation(value = "删除文件夹",httpMethod = "POST",response = ResultInfo.class,notes = "删除文件夹")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文件id",paramType = "query",required = true, dataType = "Long"),
    })
    @RequestMapping(path = "/deleteMonitorRegion",method = RequestMethod.POST)
    public ResultInfo deleteMonitorRegion(Long id, @RequestAttribute String userid) {
        try {
            logger.info("删除文件夹接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("文件id不能为空");
            }
            dbMonitorRegionService.deleteMonitorRegion(id,userid);
            return ResultInfo.renderSuccess("删除文件夹成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("删除文件夹失败");
    }

    @ApiOperation(value = "新增文件夹下探头列表查询",httpMethod = "POST",response = ResultInfo.class,notes = "新增文件夹下探头列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraName", value = "探头名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraCode", value = "探头编号", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraAreaid", value = "所属区域", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStreetid", value = "所属街道", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraManufacturer", value = "厂商", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStatus", value = "探头状态", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态", paramType = "query",required = false, dataType = "String"),

            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "id",value = "文件id",paramType = "query",required = true, dataType = "Long")
    })
    @RequestMapping(path = "/queryCameraList",method = RequestMethod.POST)
    public ResultInfo queryCameraList(Integer page,Integer limit,Long id) {
        try {
            logger.info("新增文件夹下探头列表查询接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (page == null){
                return ResultInfo.renderError("当前页码不能为空");
            }
            if (limit == null){
                return ResultInfo.renderError("每页显示条数不能为空");
            }
            if (null == id) {
                return ResultInfo.renderError("文件id不能为空");
            }
            String cameraName = StrUtils.isCheckNull(request.getParameter("cameraName"));
            String cameraCode = StrUtils.isCheckNull(request.getParameter("cameraCode"));
            String cameraAreaid = StrUtils.isCheckNull(request.getParameter("cameraAreaid"));
            String cameraStreetid = StrUtils.isCheckNull(request.getParameter("cameraStreetid"));
            String cameraManufacturer = StrUtils.isCheckNull(request.getParameter("cameraManufacturer"));
            String cameraStatus = StrUtils.isCheckNull(request.getParameter("cameraStatus"));
            String onlineStatus = StrUtils.isCheckNull(request.getParameter("onlineStatus"));


            Page<ProPageList> resultPage = dbMonitorRegionService.queryCameraList(cameraName,cameraCode,cameraAreaid,cameraStreetid,cameraManufacturer,cameraStatus,onlineStatus,page,limit,id);
            return ResultInfo.renderSuccess(resultPage);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("新增文件夹下探头列表查询失败");
    }

    @ApiOperation(value = "查看探头源数据详情信息",httpMethod = "POST",response = ResultInfo.class,notes = "查看探头源数据详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "探头id",paramType = "query",required = false, dataType = "Long"),
            @ApiImplicitParam(name = "code",value = "探头唯一编码",paramType = "query",required = false, dataType = "String")
    })
    @RequestMapping(path = "/getOriginalDetails",method = RequestMethod.POST)
    public ResultInfo getOriginalDetails() {
        try {
            logger.info("查看探头源数据详情信息接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            String id = StrUtils.isCheckNull(request.getParameter("id"));
            String code = StrUtils.isCheckNull(request.getParameter("code"));
            Map<String,Object> resultMap = dbMonitorRegionService.getOriginalDetails(id,code);
            return ResultInfo.renderSuccess(resultMap);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查看探头源数据详情信息失败");
    }

    @ApiOperation(value = "查看探头基础数据详情信息",httpMethod = "POST",response = ResultInfo.class,notes = "查看探头基础数据详情信息")
    @ApiImplicitParam(name = "id",value = "探头id",paramType = "query",required = true, dataType = "Long")
    @RequestMapping(path = "/getBasisDetails",method = RequestMethod.POST)
    public ResultInfo getBasisDetails(Long id) {
        try {
            logger.info("查看探头基础数据详情信息接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("探头id不能为空");
            }
            Map<String,Object> resultMap = dbMonitorRegionService.getBasisDetails(id);
            return ResultInfo.renderSuccess(resultMap);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查看探头基础数据详情信息失败");
    }

    @ApiOperation(value = "根据探头编号查询视频播放地址/场景图片",httpMethod = "POST",response = ResultInfo.class,notes = "根据探头编号查询视频播放地址/场景图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "探头id",paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "protocal",value = "取流协议(默认rtsp,共同协议：rtsp,rtmp,hls,海康额外有：hik,ws,大华额外有：FLV_HTTP)",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "subType",value = "码流类型(只有大华有 0:主码流,1:辅流1,2:辅流2。默认为0主码流)",paramType = "query",required = false,dataType = "int"),
            @ApiImplicitParam(name = "type",value = "查询类型(1:视频播放地址，2:场景图片)",paramType = "query",required = true, dataType = "int")
    })
    @RequestMapping(path = "/getCameraVideoUrl",method = RequestMethod.POST)
    public ResultInfo getCameraVideoUrl(Long id,Integer type) {
        try {
            logger.info("根据探头编号查询视频播放地址/场景图片接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("探头id不能为空");
            }
            if (null == type) {
                return ResultInfo.renderError("查询类型不能为空");
            }
            String protocal = StrUtils.isCheckNull(request.getParameter("protocal"));
            String subType = StrUtils.isCheckNull(request.getParameter("subType"));
            Map<String,Object> resultMap = dbMonitorRegionService.getCameraVideoUrl(id,type,protocal,subType);
            return ResultInfo.renderSuccess(resultMap);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("根据探头编号查询视频播放地址/场景图片失败");
    }

    @ApiOperation(value = "视频截图",httpMethod = "POST",response = ResultInfo.class,notes = "视频截图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "探头id",paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "url",value = "视频播放地址url",paramType = "query",required = true, dataType = "String")
    })
    @RequestMapping(path = "/screenshots",method = RequestMethod.POST)
    public ResultInfo screenshots(Long id,String url) {
        try {
            logger.info("视频截图接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("探头id不能为空");
            }
            if (StrUtils.isNull(url)) {
                return ResultInfo.renderError("视频播放地址url不能为空");
            }
            Map<String,Object> resultMap = dbMonitorRegionService.screenshots(id,url);
            return ResultInfo.renderSuccess(resultMap);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("视频截图失败");
    }

    @ApiOperation(value = "查看探头历史切片",httpMethod = "POST",response = ResultInfo.class,notes = "查看探头历史切片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "探头id", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "stime", value = "开始时间(yyyyMMddHHmmss)", paramType = "query", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "etime", value = "结束时间(yyyyMMddHHmmss)", paramType = "query", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", required = false, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query", required = false, dataType = "int"),

    })
    @RequestMapping(path = "/getBasisDetailPicture",method = RequestMethod.POST)
    public ResultInfo getBasisDetailPicture(Long id, Integer page, Integer limit) {
        try {
            logger.info("查看探头历史切片接收的参数："+ JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("探头id不能为空");
            }
            String stime = StrUtils.isCheckNull(request.getParameter("stime"));
            String etime = StrUtils.isCheckNull(request.getParameter("etime"));

            Map<String,Object> resultMap = dbMonitorRegionService.getBasisDetailPicture(id, stime, etime, page, limit);
            return ResultInfo.renderSuccess(resultMap);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查看探头历史切片失败");
    }

    @ApiOperation(value = "获取视频回放URL",httpMethod = "GET",response = ResultInfo.class,notes = "获取视频回放URL")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "探头id",paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "stime",value = "回放开始时间(yyyy-MM-dd HH:mm:ss)",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "etime",value = "回放结束时间(yyyy-MM-dd HH:mm:ss)",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "scheme",value = "协议类型，共有：rtsp、rtmp、hls三种,海康额外：hik,ws(海康默认hik,大华默认rtsp)",paramType = "query", required = false, dataType = "String")
    })
    @RequestMapping(path = "/queryPlaybackURL",method = RequestMethod.GET)
    public ResultInfo queryPlaybackURL(Long id,String stime,String etime,String scheme) {
        try {
            logger.info("获取视频回放URL请求参数为：" + com.alibaba.fastjson.JSON.toJSONString(request.getParameterMap()));
            if (null == id) {
                return ResultInfo.renderError("探头id不能为空");
            }
            if (StrUtils.isNull(stime)) {
                return ResultInfo.renderError("回放开始时间不能为空");
            }
            if (StrUtils.isNull(etime)) {
                return ResultInfo.renderError("回放结束时间不能为空");
            }
            Map<String,Object>  resultMap = dbMonitorRegionService.queryPlaybackURL(id,stime,etime,scheme);
            return ResultInfo.renderSuccess(resultMap);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return  ResultInfo.renderError("获取视频回放URL失败");
    }
}
